package com.lufei.message_system.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lufei.message_system.Data.Question;
import com.lufei.message_system.Data.PageResult;
import com.lufei.message_system.Data.QuestionVD.QuestionCreateDTO;
import com.lufei.message_system.Data.QuestionVD.QuestionUpdateDTO;
import com.lufei.message_system.Data.QuestionVD.QuestionVO;
import com.lufei.message_system.Data.Result;
import com.lufei.message_system.Mapper.QuestionMapper;
import com.lufei.message_system.Service.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.extension.toolkit.Db.saveBatch;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    //创建新问题实现
    @Override
    public Result createQuestion(QuestionCreateDTO createDTO) {
        Question question = new Question();
        BeanUtils.copyProperties(createDTO, question);

        int result = questionMapper.insert(question);
        if (result > 0) {
            return Result.success("问题创建成功");
        }
        return Result.error("问题创建失败");
    }

    @Override
    public Result batchCreateQuestions(List<QuestionCreateDTO> createDTOs) {
        if (createDTOs == null || createDTOs.isEmpty()) {
            return Result.error("问题列表不能为空");
        }

        List<Question> questions = createDTOs.stream().map(dto -> {
            Question question = new Question();
            BeanUtils.copyProperties(dto, question);
            return question;
        }).collect(Collectors.toList());

        // 使用MyBatis Plus的批量插入
        boolean success = saveBatch(questions);

        if (success) {
            return Result.success("批量创建问题成功，共创建" + questions.size() + "条问题");
        }
        return Result.error("批量创建问题失败");
    }

    //获取问题详情实现
    @Override
    public Result<QuestionVO> getQuestion(Long id) {
        Question question = questionMapper.selectById(id);
        if (question == null) {
            return Result.error("问题不存在");
        }

        QuestionVO vo = new QuestionVO();
        BeanUtils.copyProperties(question, vo);
        return Result.success(vo);
    }

    //更新问题实现
    @Override
    public Result updateQuestion(Long id, QuestionUpdateDTO updateDTO) {
        Question question = questionMapper.selectById(id);
        if (question == null) {
            return Result.error("问题不存在");
        }

        BeanUtils.copyProperties(updateDTO, question);
        int result = questionMapper.updateById(question);
        if (result > 0) {
            return Result.success("问题更新成功");
        }
        return Result.error("问题更新失败");
    }

    //删除问题实现
    @Override
    public Result deleteQuestion(Long id) {
        int result = questionMapper.deleteById(id);
        if (result > 0) {
            return Result.success("问题删除成功");
        }
        return Result.error("问题删除失败");
    }

    //分页查询问题实现
    @Override
    public Result<PageResult<QuestionVO>> listQuestions(Integer page, Integer size, String content, String dimension) {
        try {
            QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
            if (content != null && !content.isEmpty()) {
                queryWrapper.like("content", content);
            }
            if (dimension != null && !dimension.isEmpty()) {
                queryWrapper.eq("dimension", dimension);
            }

            // 创建分页对象
            Page<Question> pageInfo = new Page<>(page, size);
            questionMapper.selectPage(pageInfo, queryWrapper);

            // 转换为VO对象
            List<QuestionVO> vos = pageInfo.getRecords().stream().map(question -> {
                QuestionVO vo = new QuestionVO();
                BeanUtils.copyProperties(question, vo);
                return vo;
            }).collect(Collectors.toList());

            // 构建分页结果
            PageResult<QuestionVO> pageResult = new PageResult<>();
            pageResult.setList(vos);
            pageResult.setTotal(pageInfo.getTotal());
            pageResult.setPage(page);
            pageResult.setSize(size);

            return Result.success(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取问题列表失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<QuestionVO>> getAllQuestions() {
        try {
            // 获取所有题目
            List<Question> questions = questionMapper.selectList(null);
            
            // 转换为VO对象
            List<QuestionVO> vos = questions.stream().map(question -> {
                QuestionVO vo = new QuestionVO();
                BeanUtils.copyProperties(question, vo);
                return vo;
            }).collect(Collectors.toList());
            
            return Result.success(vos);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取题目列表失败：" + e.getMessage());
        }
    }
}
